package ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Dailog.InputDialog;
import adapter.AmendNoteAdapter;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.NoteCategoryBean;
import student.andlei.com.student.R;
import utils.GsonUtils;

/**
 * 修改笔记分类
 */
public class AmendNoteActivity extends BaseNetActivity{
    private RecyclerView recyclerView;
    private Button bt_new,
                    bt_move;
    private AmendNoteAdapter adapter;
    private int note_id;
    private int category_id;
    private String category_name = "";
    //private List<ItemBean> list = new ArrayList<>();
    private List<NoteCategoryBean> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("修改分类");
        loadData();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if(baseBean.code == 200){
            if(action.equals("note/categoryList")){
                list = GsonUtils.getInstance(AmendNoteActivity.this).parseNoHeaderJArray(baseBean, NoteCategoryBean.class);
                for(NoteCategoryBean bean:list){
                    if(category_id == bean.category_id){
                        bean.isSelect = 1;
                    }
                }
                adapter.setNewData(list);
            }else if(action.equals("note/addCategory")){
                loadData();
            }else if(action.equals("note/moveNote")){
                Intent intent = new Intent();
                intent.putExtra("category_id",category_id);
                intent.putExtra("note_id",note_id);
                intent.putExtra("category_name",category_name);
                setResult(2,intent);
                finish();
            }

        }else {
            toast(baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_amend_note;
    }

    @Override
    protected void findViews() {
        recyclerView = getView(R.id.rcly_);
        bt_new = getView(R.id.bt_new);
        bt_move = getView(R.id.bt_move);
    }

    @Override
    protected void formatViews() {
        bt_new.setOnClickListener(this);
        bt_move.setOnClickListener(this);
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new AmendNoteAdapter(R.layout.item_amend_fenlei,list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                category_id = list.get(position).category_id;
                category_name = list.get(position).category_name;
               for(int i=0;i<list.size();i++){
                   if(position == i){
                       list.get(i).isSelect = 1;
                   }else {
                       list.get(i).isSelect = 0;
                   }
               }
               adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void formatData() {
//        for(int i = 0;i<10;i++){
//            ItemBean bean = new ItemBean();
//            bean.isSelect = 0;
//            bean.str = "语文"+i;
//            list.add(bean);
//        }
       // loadData();
    }
    private void loadData(){
        //获取分类列表
        post("note/categoryList", new HashMap(), NoteCategoryBean.class, true);
    }
    @Override
    protected void getBundle(Bundle bundle) {
            note_id =bundle.getInt("note_id");
            category_id = bundle.getInt("category_id");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_new:
                new InputDialog(this, R.style.dialog, new InputDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {

                    }

                    @Override
                    public void onClick(Dialog dialog, boolean confirm, EditText editText) {
                        if (confirm) {
                            Map<String, String> map = new HashMap<>();
                            map.put("name", editText.getText().toString());
                            post("note/addCategory", map, NoteCategoryBean.class, false);
                            dialog.dismiss();
                        }
                    }
                }).setTitle("新建分类").settPositiveButtonColor(R.color.homepage_titlefont).show();
                break;
            case R.id.bt_move:
                Map<String, String> map = new HashMap<>();
                map.put("note_id", note_id+"");
                map.put("category_id", category_id+"");
                post("note/moveNote", map, NoteCategoryBean.class, false);
                break;
        }
    }
}
